package thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo30 {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(1000);

        Thread produce = new Thread(()->{
            int n = 0;
            while(true){
                try {
                    queue.put(n);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("生产元素 " + n);
                n++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        },"produce");

        Thread consume = new Thread(()->{
            while(true){
                Integer n = null;
                try {
                    n = queue.take();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("消费元素 " + n);
            }
        },"consume");

        produce.start();
        consume.start();

    }
}
